
import Vue from 'vue';
import VueRouter from 'vue-router';
import HomeView from '../views/Home/HomeView.vue';
import BookDetail from '../views/Books/BookDetail.vue';
import Manager from "@/views/Manager.vue";
import authorList from "@/views/Author/AuthorList.vue";

// 修复路由重复点击报错问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'Manager',
    component: Manager,
    children: [
      {
        path: '/', // 空字符串表示根路径
        redirect: '/home',
      },
      {
        path: 'home', // 空字符串表示根路径
        name: 'Home',
        component: HomeView,
      },
      {
        path: '/bookdetail/:bookType',
        name: 'BookDetail',
        component: BookDetail,
        props: true // 允许将查询参数作为 props 传递给组件
      },

      {
        path: 'tag/:tag',
        name: 'BookList',
        component: () => import('@/views/Tag/TagBookList.vue'),
        props: true, // 允许将路由参数作为 props 传递给组件
      },
      {
        path: 'tag',
        name: 'TagList',
        component: () => import('@/views/Tag/TagList.vue'),
      },
      {
        path: 'allbooklist',
        name: 'AllBooksList',
        component: () => import('@/views/Books/AllBooksList.vue'),
      },
      {
        path: 'publisherbooklist/:publisher',
        name: 'PublisherBookList',
        component: () => import('@/views/Publisher/PublisherBookList.vue'),
        props: true,
      },

      {
        path: 'publisherList',
        name: 'PublisherList',
        component: () => import('@/views/Publisher/PublisherList.vue'),
      },
      {
        path: 'author',
        name: 'AuthorList',
        component: authorList,
      },
      {
        path: '/authordetail/:name',
        name: 'AuthorDetail',
        component: () => import('@/views/Author/AuthorDetail.vue'),
        props: true, // 允许将路由参数作为 props 传递给组件
      },
      {
        path: '/search/:query',
        name: 'SearchResults',
        component: () => import('@/views/Search/SearchResults.vue'),
        props: true,
      },
      {
        path: '/about',
        name:'About',
        component: () => import('@/views/About.vue'),
      },
      {
        path: '/:pathMatch(.*)*', // 匹配所有不存在的路径
        name: 'NotFound',
        component: () => import('@/components/404.vue'),
      },
    ],
  },
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  // 每次路由切换前滚动到顶部
  window.scrollTo(0, 0);
  next();
});

export default router;